package users;

import java.io.Serializable;

import networkedmodules.Module;
import networkedmodules.ModuleGroup;
import networkedmodules.Update;

public class UserPermissionsUpdate extends Update implements Serializable
{
    private static final long serialVersionUID = 1L;
    UserGroup userGroup;

    public UserPermissionsUpdate(String key, UserGroup userGroup)
    {
        setKey(key);
        this.userGroup = userGroup;
    }

    public void performUpdate(Module module)
    {
        UserGroup group = (UserGroup) module;
        if (module.getModuleGroup().getType() == ModuleGroup.HOST)
        {
            // We update corresponding copies
            for (User user : group.getUsers())
                for (User remoteUser : userGroup.getUsers())
                    if (user.equals(remoteUser))
                        user.setUserPermissions(remoteUser.getUserPermissions());
            group.getModuleGroup().sendUpdate(
                    new UserPermissionsUpdate(group.getKey(), group));
        }
        else if (module.getModuleGroup().getType() == ModuleGroup.CLIENT)
        {
            // We update corresponding copies
            for (User user : group.getUsers())
                for (User remoteUser : userGroup.getUsers())
                    if (user.equals(remoteUser))
                        user.setUserPermissions(remoteUser.getUserPermissions());
        }
    }
}
